﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using zevenseas.LifeCycleManagement.Utilities;


namespace zevenseas.LifeCycleManagement.ApplicationPages
{
    public class OverviewWebApplications : ApplicationsManagementPage
    {
        protected string totalContentDatabases = string.Empty;
        protected string totalSiteCollections = string.Empty;
        protected string totalSize = string.Empty;
        protected string webApplicationName = string.Empty;

        protected WebApplicationSelector webApplicationSelector;
        protected SPGridView webApplicationGridView;    
        
        protected override void CreateChildControls()
        {
            if (webApplicationSelector != null)
            {
                //Bind the data
                SPWebApplication webApplication = webApplicationSelector.CurrentItem;
                
                //Populate the headers
                webApplicationName = webApplication.Name.ToString();
                totalSiteCollections = webApplication.Sites.Count.ToString();
                totalContentDatabases = webApplication.ContentDatabases.Count.ToString();
                ulong totalsize = new ulong();
                
                foreach (SPContentDatabase contentDatabase in webApplication.ContentDatabases)
                {
                    totalsize = totalsize + contentDatabase.DiskSizeRequired;
                }
                
                totalSize = (totalsize / 0x400L / 0x400L).ToString();

                webApplicationGridView.ID = "webApplicationGridView";
                webApplicationGridView.AutoGenerateColumns = false;
                webApplicationGridView.AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;
                webApplicationGridView.DataSource = GetContentDatabaseData();
                webApplicationGridView.DataBind();
            }

            base.CreateChildControls();
        }

        private DataTable GetContentDatabaseData()
        {
            DataTable webApplicationsTable = Utilities.Utilities.WebApplicationDataTable;
            foreach (SPContentDatabase contentDatabase in webApplicationSelector.CurrentItem.ContentDatabases)
            {
                DataRow row = webApplicationsTable.NewRow();
                row["ContentDatabase"] = contentDatabase.Name.ToString();
                row["Diskspace"] = (contentDatabase.DiskSizeRequired / 0x400L / 0x400L).ToString();                
                row["SiteCount"] = contentDatabase.Sites.Count.ToString();
                row["Server"] = contentDatabase.Server.ToString();
                webApplicationsTable.Rows.Add(row);
            }

            return webApplicationsTable;

        }
    }
}
